/**
* @title: 注册页
* @description:
* @author JUN
*/
<template>
	<view class="register page">
		<!-- 背景 -->
		<bg class="bg"></bg>

		<!-- 中间区域 -->
		<view class="box">
			<!-- 授权用户信息弹窗 -->
			<get-user-info @allowGetUserInfo="allowGetUserInfo($event)" :showbtn="showbtn"></get-user-info>

			<!-- 注册表单 -->
			<register-form :userMassage="userMassage" :openid="openid" :sessionKey="sessionKey"></register-form>
		</view>

		<!-- 消息提示 -->
		<u-toast ref="uToast" />
		
		<wyb-loading ref="loading"/>
	</view>
</template>

<script>
	import * as api from "@/api/apiList/register.js"

	import {
		createNamespacedHelpers
	} from 'vuex'
	const {
		mapMutations
	} = createNamespacedHelpers('userModule')

	import bg from "./components/bg.vue"
	import registerForm from "./components/registerForm.vue"
	import getUserInfo from "./components/getUserInfo.vue"

	export default {
		components: {
			bg,
			registerForm,
			getUserInfo
		},
		data() {
			return {
				userMassage: "", //用户信息
				showbtn: false, //是否开启用户授权提示 false:默认关闭，true:开启
				openid: "",
				sessionKey: ""
			}
		},
		methods: {
			...mapMutations(["GET_TOKEN"]),
			allowGetUserInfo(e) { //允许获取用户信息
				this.userMassage = e
			},
			async API_wxLogin(code) { //API微信登录
				let result = {
					param: {
						code
					}
				}
				return await api.wxLogin(result)
			},
			async API_login(wxid) { //登录接口
				let result = {
					param: {
						wxid //openid
					}
				}
				return await api.login(result)
			},
			WX_login() { //微信登录
				return new Promise((resolve) => {
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							resolve(loginRes)
						}
					});
				})
			},
			getSetting() { //授权
				return new Promise((resolve) => {
					uni.getSetting({
						success(res) {
							console.log()
							resolve(res)
						}
					})
				})
			},
			getUserInfo() { //获取用户信息
				return new Promise((resolve) => {
					uni.getUserInfo({
						success(res) {
							resolve(res)
						}
					})
				})
			},
			async isLogin() { //判断是否已经注册过,注册过直接登录
				let loginRes = await this.WX_login() //微信登录

				let loginApiRes = await this.API_wxLogin(loginRes.code) //api微信登录

				this.openid = loginApiRes.data.data.openid //获取openid
				// this.openid = "oiWGr5VXNsPtswUN731adyLzXh6k"

				this.sessionKey = loginApiRes.data.data.sessionKey //获取sessionKey

				let res = await this.API_login(this.openid) //登录接口
				return res
			}
		},
		async created() {
			// 页面逻辑:
			// 	1)进入页面先判断是否注册过,如果注册过则直接进入等待页
			// 	2)没有注册过则弹出授权窗口，确认授权后显示基本信息
			this.$nextTick(()=>{
				this.$refs.loading.showLoading() // 显示
			})
			
			let res = await this.isLogin() //判断是否注册过
			console.log(res)
			if (res.data.code === 1000) { //注册过则进入等待页
				this.GET_TOKEN(res.data.data) //存token
				
				this.$refs.uToast.show({ //提示
					title: res.data.msg,
					type: 'success'
				})
				uni.navigateTo({ //跳转到等待页
					url: '/pages/waitting/waitting'
				});
			}else {
				// this.onReady = true //显示loading效果
				this.showbtn = true //开启用户授权弹窗
				console.log(this.showbtn)
			}
			
			this.$refs.loading.hideLoading() // 隐藏
		}
	}
</script>

<style lang="scss" scoped>
	.register {
		.bg {
			//背景
			position: absolute;
			z-index: -999;
			width: 100%;
			height: 100%;
		}

		.box {
			//中间区域
			position: absolute;
			z-index: 1;
			width: 100%;
			height: 100%;
		}
	}
</style>
